package com.comp.controller;

import com.comp.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

@Controller
@RequestMapping("/test2")
public class Test2Controller {

    @RequestMapping("/test21")
    public String test1() {
        System.out.println("test21");
        return "test1";
    }

    @RequestMapping("/test22")
    public String test2() {
        System.out.println("test22");
        return "/test2/test1";
    }

    @RequestMapping("/test3")
    public ModelAndView test3() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("str","hello world");
        mv.setViewName("/test2/test3");
        return mv;
    }

    @RequestMapping("/test4/{id}")
    public ModelAndView test4(@PathVariable String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("str",id);
        mv.setViewName("/test2/test4");
        return mv;
    }

    // Rest风格
    @RequestMapping("/test5/{id}/{name}")
    public ModelAndView test5(@PathVariable String id,@PathVariable String name) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("id",id);
        mv.addObject("name",name);
        mv.setViewName("/test2/test5");
        return mv;
    }

    @RequestMapping("/test6")
    @ResponseBody
    public String test6() {
        return "/test2/test6";
    }

    @RequestMapping("/test7")
    @ResponseBody
    public List test7() {
        List list = new ArrayList();
        list.add("item1");
        list.add("item2");
        list.add("item3");
        list.add("item4");
        return list;
    }

    @RequestMapping("/test8")
    @ResponseBody
    public Map test8() {
        Map map = new HashMap();
        map.put("key1","value1");
        map.put("key2","value2");
        map.put("key3","value3");
        map.put("key4","value4");
        return map;
    }

    @RequestMapping("/test9")
    @ResponseBody
    public List test9() {
        List list = new ArrayList();

        Map map = new LinkedHashMap();
        map.put("username","张三");
        map.put("city","新乡");
        map.put("age",18);
        list.add(map);

        map = new LinkedHashMap();
        map.put("username","李四");
        map.put("city","郑州");
        map.put("age",19);
        list.add(map);

        map = new LinkedHashMap();
        map.put("username","小明");
        map.put("city","开封");
        map.put("age",17);
        list.add(map);

        return list;
    }

    @RequestMapping("/test10")
    @ResponseBody
    public List test10() {
        List list = new ArrayList();

        User user = new User("user1","123");
        list.add(user);

        user = new User("user2","1234");
        list.add(user);

        user = new User("user3","123456");
        list.add(user);

        return list;
    }

    @RequestMapping("/test11")
    @ResponseBody
    public User test11() {
        User user = new User("user1","123");

        return user;
    }
}
